import warnings
warnings.filterwarnings("ignore")
import numpy as np
import time
from pandas import read_excel
from pandas import DataFrame
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from xgboost import plot_importance
from xgboost import XGBRegressor
# https://www.cnblogs.com/gdjlc/p/11409804.html

randomIndex = (5, 14, 22, 30, 37, 43, 60, 72, 75, 77, 96, 107, 120, 123, 164, 166, 172, 183, 194, 214, 216, 219, 229, 234, 265, 276, 279, 300, 310, 343, 353, 356, 358, 360, 367, 369, 380, 387, 388, 391, 400, 442, 453, 456, 469, 470, 484, 511, 523, 524, 531, 532, 534, 561, 570, 572, 574, 588, 610, 615, 618, 632, 651, 659, 661, 674, 681, 682, 688, 697, 704, 711, 718, 720, 745, 766, 783, 794, 796, 800, 801, 805, 820, 832, 834, 841, 845, 852, 860, 899, 901, 911, 915, 917, 924, 930, 935, 943, 954, 955, 965, 978, 1032, 1046, 1058, 1062, 1063, 1077, 1115, 1123, 1166, 1190, 1196, 1201, 1213, 1224, 1235, 1238, 1255, 1277, 1294, 1301, 1311, 1316, 1323, 1328, 1368, 1370, 1375, 1388, 1401, 1411, 1423, 1445, 1496, 1498, 1510, 1512, 1517, 1519, 1532, 1557, 1562, 1595, 1603, 1604, 1606, 1615, 1622, 1654, 1662, 1675, 1685, 1692, 1693, 1695, 1698, 1699, 1712, 1716, 1717, 1726, 1728, 1737, 1738, 1740, 1747, 1755, 1756, 1761, 1763, 1765, 1771, 1779, 1780, 1790, 1795, 1807, 1815, 1818, 1819, 1825, 1834, 1855, 1878, 1880, 1897, 1898, 1903, 1922, 1940, 1942, 1948, 1977, 1979, 1981, 2000, 2003, 2011, 2048, 2049, 2057, 2077, 2078, 2092, 2100, 2109, 2120, 2154, 2165, 2168, 2183, 2197, 2198, 2207, 2223, 2238, 2250, 2254, 2257, 2269, 2274, 2275, 2300, 2309, 2315, 2321, 2342, 2352, 2363, 2365, 2375, 2384, 2389, 2398, 2423, 2425, 2433, 2453, 2455, 2457, 2465, 2474, 2483, 2485, 2486, 2487, 2499, 2506, 2513, 2551, 2576, 2583, 2597, 2608, 2615, 2630, 2631, 2634, 2647, 2689, 2694, 2713, 2723, 2725, 2734, 2735, 2744, 2748, 2753, 2758, 2782, 2807, 2849, 2865, 2869, 2876, 2892, 2897, 2907, 2909, 2926, 2927, 2956, 2967, 2978, 2998, 3000, 3003, 3026, 3027, 3048, 3059, 3078, 3085, 3089, 3111, 3120, 3125, 3139, 3147, 3152, 3160, 3179, 3196, 3207, 3208, 3226, 3228, 3245, 3254, 3263, 3277, 3279, 3287, 3301, 3305, 3309, 3317, 3318, 3323, 3336, 3343, 3351, 3360, 3361, 3386, 3407, 3428, 3431, 3432, 3443, 3457, 3461, 3462, 3470, 3472, 3474, 3492, 3498, 3512, 3537, 3554, 3587, 3596, 3630, 3639, 3648, 3672, 3676, 3677, 3686, 3688, 3711, 3712, 3722, 3735, 3739, 3740, 3745, 3746, 3752, 3755, 3758, 3786, 3791, 3804, 3812, 3814, 3817, 3834, 3843, 3860, 3865, 3891, 3892, 3908, 3926, 3938, 3939, 3948, 3953, 3975, 3976, 3983, 3990, 4014, 4050, 4063, 4072, 4075, 4081, 4083, 4089, 4092, 4106, 4110, 4111, 4114, 4116, 4119, 4123, 4136, 4155, 4180, 4184, 4185, 4190, 4206, 4217, 4221, 4230, 4232, 4234, 4237, 4247, 4268, 4271, 4276, 4296, 4301, 4305, 4309, 4332, 4336, 4343, 4350, 4358, 4365, 4368, 4369, 4371, 4377, 4380, 4381, 4417, 4427, 4431, 4438, 4440, 4445, 4474, 4485, 4509, 4522, 4525, 4529, 4534, 4536, 4540, 4548, 4552, 4567, 4575, 4583, 4603, 4607, 4611, 4622, 4629, 4643, 4660, 4673, 4679, 4706, 4707, 4714, 4737, 4738, 4753, 4756, 4782, 4785, 4801, 4816, 4819, 4832, 4843, 4860, 4877, 4901, 4910, 4922, 4923, 4926, 4928, 4939, 4941, 4942, 4946, 4957, 4973, 5011, 5014, 5015, 5024, 5029, 5046, 5061, 5095, 5120, 5125, 5126, 5132, 5145, 5158, 5161, 5169, 5188, 5202, 5205, 5207, 5215, 5221, 5233, 5236, 5287, 5292, 5293, 5321, 5330, 5331, 5343, 5346, 5348, 5352, 5393, 5398, 5400, 5405, 5419, 5430, 5432, 5436, 5437, 5440, 5441, 5451, 5461, 5463, 5468, 5498, 5501, 5503, 5508, 5509, 5566, 5574, 5575, 5577, 5609, 5617, 5655, 5658, 5693, 5719, 5721, 5725, 5732, 5735, 5737, 5740, 5746, 5750, 5772, 5774, 5790, 5796, 5800, 5807, 5835, 5848, 5849, 5857, 5866, 5871, 5888, 5919, 5926, 5952, 5957, 5963, 5977, 5984, 5987, 5991, 6003, 6014, 6017, 6044, 6053, 6056, 6069, 6078, 6092, 6106, 6128, 6132, 6141, 6169, 6172, 6197, 6199, 6206, 6217, 6220, 6228, 6243, 6256, 6258, 6277, 6280, 6285, 6288, 6314, 6318, 6328, 6334, 6346, 6349, 6355, 6357, 6371, 6378, 6388, 6389, 6395, 6400, 6416, 6447, 6461, 6467, 6502, 6504, 6516, 6558, 6568, 6569, 6574, 6583, 6593, 6596, 6608, 6609, 6623, 6625, 6635, 6646, 6656, 6665, 6673, 6675, 6688, 6721, 6735, 6740, 6768, 6770, 6773, 6780, 6789, 6800, 6803, 6806, 6810, 6813, 6818, 6840, 6842, 6847, 6849, 6853, 6865, 6897, 6904, 6918, 6922, 6935, 6945, 6952, 6962, 6981, 6991, 6996, 7003, 7017, 7021, 7031, 7036, 7039, 7048, 7051, 7052, 7057, 7070, 7071, 7084, 7090, 7120, 7129, 7136, 7142, 7143, 7150, 7158, 7172, 7175, 7179, 7191, 7200, 7204, 7224, 7229, 7230, 7231, 7251, 7256, 7261, 7262, 7271, 7287, 7295, 7310, 7311, 7337, 7338, 7341, 7342, 7371, 7415, 7422, 7433, 7436, 7441, 7458, 7471, 7473, 7474, 7488, 7505, 7511, 7515, 7545, 7551, 7553, 7568, 7576, 7582, 7583, 7584, 7598, 7617, 7626, 7632, 7640, 7673, 7688, 7699, 7726, 7743, 7746, 7766, 7771, 7777, 7810, 7821, 7822, 7832, 7836, 7850, 7857, 7864, 7869, 7876, 7891, 7894, 7903, 7911, 7913, 7914, 7924, 7957, 7967, 7974, 7978, 7992, 7994, 7996, 7999, 8000, 8001, 8013, 8037, 8042, 8044, 8045, 8047, 8052, 8060, 8076, 8092, 8093, 8110, 8143, 8160, 8168, 8170, 8171, 8204, 8212, 8218, 8223, 8234, 8237, 8249, 8250, 8251, 8254, 8265, 8266, 8268, 8278, 8283, 8296, 8298, 8301, 8302, 8335, 8349, 8355, 8373, 8395, 8413, 8416, 8434, 8436, 8441, 8448, 8452, 8460, 8464, 8475, 8479, 8482, 8492, 8500, 8517, 8518, 8519, 8524, 8528, 8530, 8533, 8538, 8544, 8558, 8559, 8565, 8571, 8603, 8615, 8623, 8629, 8639, 8645, 8662, 8670, 8680, 8686, 8693, 8711, 8717, 8726, 8732, 8741, 8753, 8757, 8770, 8775, 8779, 8816, 8833, 8870, 8875, 8885, 8892, 8902, 8913, 8914, 8922, 8931, 8939, 8940, 8970, 8971, 8978, 8979, 8984, 8995, 9017, 9018, 9019, 9024, 9039, 9051, 9056, 9073, 9076, 9095, 9097, 9098, 9099, 9103, 9107, 9120, 9123, 9124, 9125, 9141, 9163, 9164, 9167, 9173, 9190, 9212, 9245, 9256, 9263, 9291, 9311, 9322, 9325, 9328, 9333, 9337, 9341, 9352, 9368, 9380, 9414, 9417, 9419, 9437, 9440, 9443, 9459, 9461, 9470, 9475, 9481, 9482, 9510, 9530, 9533, 9542, 9543, 9549, 9554, 9567, 9580, 9585, 9586, 9600, 9603, 9613, 9622, 9626, 9633, 9642, 9656, 9669, 9670, 9678, 9679, 9683, 9705, 9710, 9732, 9748, 9755, 9791, 9802, 9817, 9824, 9833, 9836, 9844, 9860, 9866, 9875, 9879, 9881, 9890, 9894, 9914, 9940, 9968, 9981, 9988, 9994)

df = read_excel(r'C:\Users\zll\Desktop\JMS_\RawData.xlsx',Sheetname='Sheet1',header=0 )
len1 = df['Case ID'].__len__()
continuous_conds = df.ix[:,['StartTime1',  'EndTime1', 'Cost1',
             'StartTime2',  'EndTime2', 'Cost2',
             'StartTime3',  'EndTime3', 'Cost3',
             'StartTime4',  'EndTime4', 'Cost4',
             'StartTime5',  'EndTime5', 'Cost5',
             'StartTime6',  'EndTime6', 'Cost6',
             'StartTime7',  'EndTime7', 'Cost7',
             'StartTime8',  'EndTime8', 'Cost8',
             'StartTime9',  'EndTime9', 'Cost9'
            ]].values
endtime11  = df['EndTime11'].values
endtime10 = df['EndTime10'].values
endtime = np.zeros((len1))

total_time = []

continuous_res = np.zeros([len1, 27])
for i in range(len1):
    for j in range(27):
        if type(continuous_conds[i, j]) == str:
                continuous_res[i, j] = int(continuous_conds[i, j].split(":")[0]) * 3600 + int(continuous_conds[i, j].split(":")[1]) * 60 + int(continuous_conds[i, j].split(":")[2])
        elif type(continuous_conds[i, j]) == int or type(continuous_conds[i, j]) == float:
            continuous_res[i, j] = continuous_conds[i, j]
        else:
            continuous_res[i, j] = continuous_conds[i, j].hour *  3600 + continuous_conds[i, j].minute * 60 + continuous_conds[i, j].second
    if type(endtime11[i]) == str:
        endtime[i] = int(endtime11[i].split(":")[0]) * 3600 + int(endtime11[i].split(":")[1]) * 60 + int(endtime11[i].split(":")[2])
    elif type(endtime10[i]) == str:
        endtime[i] = int(endtime10[i].split(":")[0]) * 3600 + int(endtime10[i].split(":")[1]) * 60 + int(endtime10[i].split(":")[2])
    elif endtime10[i] == 0:
        endtime[i] = endtime11[i].hour * 3600 + endtime11[i].minute * 60 + endtime11[i].second
    else:
        endtime[i] = endtime10[i].hour * 3600 + endtime10[i].minute * 60 + endtime10[i].second
    total_time.append(endtime[i] - continuous_res[i, 0])
total_time = np.array(total_time)

dispersed_conds = df.ix[:,[
                           'Register_Request',  'Reason1', 'Resource1',   #1, 3, 1   5
                           'Date_Check', 'Type2', 'Resource2',            #1, 2, 1   9
                           'Mode_Audit', 'Resource3',   #1,  1   11
                           'Manual_Review',  'Resource4', #2,  2   15
                           'Reason_Review', 'Result5', 'Resource5',  #2, 3, 2   22
                           'Ticket_Check', 'Valid6', 'Resource6',    # 2, 3, 2   29
                           'Casually_Examine', 'Result7', 'Resource7', # 2, 3, 2   36
                           'Thoroughly_Examine', 'Result8', 'Resource8', # 2, 3, 2   43
                           'Decide',  'Resource9'  # 2,  2   47
             ]].values

le = LabelEncoder()
oh = OneHotEncoder(sparse=False)
for i in range(len(dispersed_conds[0])):
    dispersed_conds[:, i] = le.fit_transform(dispersed_conds[:, i])
oh.fit(dispersed_conds)
onehot_enc = oh.transform(dispersed_conds)

#将同一index归并
arr1 = [5, 9, 11, 15, 22, 29, 36, 43, 47]
to_traineAttrs = onehot_enc[:, :5]
for i in range(1, len(arr1)):
    to_traineAttrs = np.hstack((to_traineAttrs, continuous_res[:, 3*(i-1): 3*i], onehot_enc[:, arr1[i-1]:arr1[i]]))
# to_traineAttrs = np.hstack((to_traineAttrs, continuous_res[:, 21:24], onehot_enc[:, 48:]))
res = np.hstack((to_traineAttrs, continuous_res[:, 24:]))

picked_data = []
picked_totaltime = []
for i in range(len1):
    if i in randomIndex:
        continue
    else:
        picked_data.append(res[i])
        picked_totaltime.append(total_time[i])
picked_data = np.array(picked_data)
picked_totaltime = np.array(picked_totaltime)

# xgboost
# https://www.cnblogs.com/wanglei5205/p/8578486.html
# arr1 =    [5,            9,              13,              20,              27,              34,              41,              48,             55]
mae = 0
imp_split = [ 8,  15,  20,  27,  37,  47,  57,  67,  74]
for table in [picked_data]:
    X_train, X_test, y_train, y_test = train_test_split( table, picked_totaltime, random_state=0, test_size=.2 )
    model = XGBRegressor()
    # model = RandomForestRegressor(criterion='mse', max_depth=10, random_state=0)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    mae = np.sum(np.abs(y_pred - y_test)) / (len(y_test) * 60.0)
    # for i in range(len(y_test)):
    #     mse  += (y_pred[i] - y_test[i]) * (y_pred[i] - y_test[i])
    # mse = mse * 1.0 / len(y_test)
    print("mae is : " + str(mae))
    # print(model.score(y_train, y_pred))
    # accuracy = accuracy_score(y_test, y_pred)
    # print("accuarcy: %.2f%%" % (accuracy * 100.0))

    fip = model.feature_importances_
    fips = [sum(fip[:8])]
    for tmp in range(1, len(imp_split)):
        fips.append(sum(fip[:imp_split[tmp]]) - sum(fips))
    fip_result = [format(x, '.2%') for x in fips]
    print("Feature importances:\n{}".format(fip_result))
    plt.bar([x for x in range(len(fips))], fips)
    plt.show()


print("done")